<script lang="ts" setup>
import { ref,computed } from 'vue'
import DepartmentParentSelect from '../utils/departmentParentSelect.vue';
import userManagementSelect from '../utils/userMangementSelect.vue'
import { useAddDepartmentStore } from '../../store/addDepartment';
import { useDepartmentStore } from '../../store/department';
import { useDepartmentSelectStore } from '../../store/departmentSelect';
import department from '../../api/department';
import { FormInstance } from 'element-plus'
import utils from '../../utils/utils';
const ruleFormRef = ref<FormInstance>()
const addDepartmentStore = useAddDepartmentStore()
const departmentStore = useDepartmentStore()
const departmentSelectStore = useDepartmentSelectStore()
const rules = {
}
defineExpose({
  reflashData
});
const text = computed(() => {
    return  addDepartmentStore.department.id!==''? '修改部门' : '新增部门'
})
async function reflashData() {
    await department.getAllDepartmentList().then(res => {
        departmentStore.setDepartmentTable(res.data.data)
    }).catch(err => {
        console.log(err)
    }).finally(() => {
        departmentStore.changeLoading()
    })
    await department.getDepartmentSelect().then((res) => {
        departmentSelectStore.setDepartmentSelect(res.data.data);
        console.log(departmentSelectStore.departmentSelect);
    }).catch((err) => {
        console.log(err);
    });
}
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
        if (valid) {
            //新增部门
            addDepartmentStore.department.time = utils.getCurrentTime()
            if (addDepartmentStore.department.id === '') {
                await department.addDepartment(addDepartmentStore.department).then(res => { 
                    if(res.data.code === 200){
                        utils.showMessage(res.data.message, 'success', 2000)
                    }
                    if(res.data.code === 500){
                        utils.showMessage(res.data.message, 'error', 2000)
                    }
                })
                .catch(err => {
                    console.log(err);
                }).finally(async () => {
                    addDepartmentStore.setIsOpen(false)
                    addDepartmentStore.setDepartmentNull()
                })
                reflashData()
            }
            else {
                //修改部门
                await department.updateDepartmentById(addDepartmentStore.department).catch(err => {
                    console.log(err);
                }).finally(async () => {
                    addDepartmentStore.setIsOpen(false)
                    addDepartmentStore.setDepartmentNull()
                })
                reflashData()
            }
        }
    })
}
function failedSubmit() {
    addDepartmentStore.setIsOpen(false)
    departmentSelectStore.setDisabled(false)
    addDepartmentStore.setDepartmentNull()
}
</script>
<template>
    <el-dialog v-model="addDepartmentStore.isOpen" :title="text" width="600" center>
        <el-form ref="ruleFormRef" :model="addDepartmentStore.department" :rules="rules" label-width="100px"
            class="demo-ruleForm">
            <el-row :gutted="24">
                <el-col :span="24">
                    <el-form-item
                        v-if="(addDepartmentStore.department.parentId !== addDepartmentStore.department.id) || addDepartmentStore.department.id === ''"
                        label="上级部门" prop="parentId">
                        <DepartmentParentSelect />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="部门名称" prop="deptName">
                        <el-input v-model="addDepartmentStore.department.deptName" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="状态" prop="status">
                        <el-radio-group v-model="addDepartmentStore.department.status">
                            <el-radio :value="1">正常</el-radio>
                            <el-radio :value="0">停用</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="负责人" prop="managerId">
                        <userManagementSelect />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="failedSubmit">取消</el-button>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    提交
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>
<style scoped>
.demo-ruleForm {
    margin-top: 1rem;
}
</style>